package controller.binder;

import java.beans.PropertyEditorSupport;
import java.util.ArrayList;
import java.util.List;

import cultu.community.message.Community;

/**
 * Preferences list property editor.
 * @author Marc-Antoine
 *
 */
public class CommunityListPropertyEditor extends PropertyEditorSupport
{
  /**
   * {@inheritDoc}
   */
  @Override
  public void setAsText(final String text) {
  
    // The unserialized preferences list.
    final List<Community> communitiesList = new ArrayList<Community>();
  
    // Text check.
    if (text != null && !"".equals(text)) {
      
      // Split text.
      final String[] communitiesStringArray = text.split(":");
      
      // Loop over preferences string to unseralize.
      for (String communityString : communitiesStringArray) {
        
        // Create preference and set id.
        final Community community = new Community();
        community.setHjid(Long.valueOf(communityString));
        
        // Add to list.
        communitiesList.add(community);
      }
      
    }
    
    setValue(communitiesList);
  }
}
